/**
Copyright [2009] [Julian Gamble] 

Licensed under the Apache License, Version 2.0 (the "License"); 
you may not use this file except in compliance with the License. 
You may obtain a copy of the License at 

	http://www.apache.org/licenses/LICENSE-2.0 
	
Unless required by applicable law or agreed to in writing, software 
distributed under the License is distributed on an "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
See the License for the specific language governing permissions and 
limitations under the License. 
 **/
package cruiseprojectlist.domain.coveragecomplexity;

import java.io.File;

public class CoverageComplexityUtil {

	private Handler handler;

	public CoverageComplexityUtil(String coberturaFilePath) {
		if (null != coberturaFilePath)
			if (new File(coberturaFilePath).exists())
				parseDocument(coberturaFilePath);
	}

	public double getCoverage() {
		if (handler != null) {
			java.util.List<CoverageComplexity> myCoverages = handler
					.getCoverages();
			// System.out.println("No of Employees '" + myCoverages.size() +
			// "'.");

			java.util.Iterator<CoverageComplexity> it = myCoverages.iterator();
			// assume 1 - or take first one
			while (it.hasNext()) {
				CoverageComplexity c = (CoverageComplexity) it.next();
				return (double) c.getLinescovered() / c.getLinesvalid();
			}
		}
		return 0.0;
	}

	public double getComplexity() {
		if (handler != null) {
			java.util.List<CoverageComplexity> myCoverages = handler
					.getCoverages();
			// System.out.println("No of Employees '" + myCoverages.size() +
			// "'.");

			java.util.Iterator<CoverageComplexity> it = myCoverages.iterator();
			// assume 1 - or take first one
			while (it.hasNext()) {
				CoverageComplexity c = (CoverageComplexity) it.next();
				return (double) c.getCcn();
			}
		}
		return 0.0;
	}

	private void parseDocument(String document) {

		// get a factory
		javax.xml.parsers.SAXParserFactory spf = javax.xml.parsers.SAXParserFactory
				.newInstance();
		try {

			// get a new instance of parser
			javax.xml.parsers.SAXParser sp = spf.newSAXParser();

			handler = new Handler();

			// parse the file and also register this class for call backs
			sp.parse(document, handler);

		} catch (org.xml.sax.SAXException se) {
			se.printStackTrace();
		} catch (javax.xml.parsers.ParserConfigurationException pce) {
			pce.printStackTrace();
		} catch (java.io.IOException ie) {
			ie.printStackTrace();
		}
	}

}